﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace AJArt.Test
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class Thumbnail
    {
        public Thumbnail()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void GenerateThumbnails()
        {
            const string src = @"D:\Projects\AJArt\photos";

            const string photos = @"D:\MySpaces\Projects\nport\AJArt.Mvc\AJArt.Mvc\Content\photos";
            const string thumbnails = @"D:\MySpaces\Projects\nport\AJArt.Mvc\AJArt.Mvc\Content\thumbnails";

            //遍历所有Cat
            foreach (var cat in Directory.GetDirectories(src))
            {
                DirectoryInfo di = new DirectoryInfo(cat);
                var catph = Path.Combine(photos, di.Name);
                var cattm = Path.Combine(thumbnails, di.Name);

                //如果不存在照片文件夹，创建之
                if (!Directory.Exists(catph))
                    Directory.CreateDirectory(catph);

                //Copy照片过去
                foreach (var story in di.GetDirectories())
                {
                    var storyDir = Path.Combine(catph, story.Name);

                    //如果不存在Story文件夹，创建之
                    if (!Directory.Exists(storyDir))
                        Directory.CreateDirectory(storyDir);

                    int i = 0;
                    foreach (var photo in story.GetFiles("*.jpg"))
                    {
                        if (!photo.Name.StartsWith("."))
                            photo.CopyTo(Path.Combine(storyDir, i++.ToString("00") + ".jpg"), true);
                    }
                }


                //如果不存在缩略图文件夹，创建之
                if (!Directory.Exists(cattm))
                    Directory.CreateDirectory(cattm);

                //遍历所有Story，创建缩略图
                //缩略图由甲方提供，故移除缩略图生成代码
                //foreach (var story in Directory.GetDirectories(catph))
                //{
                //    //200*130
                //    var storyDir = new DirectoryInfo(story);
                //    var cover = storyDir.GetFiles().Random();

                //    //生成缩略图
                //    Bitmap bmp = new Bitmap(new Bitmap(cover.FullName), new Size(200, 130));
                //    var thumbnail = Path.GetTempFileName();
                //    bmp.Save(thumbnail);

                //    File.Copy(thumbnail, Path.Combine(cattm, storyDir.Name + ".jpg"), true);
                //}
            }
        }
    }


}
